package itsm.isperp.module.service.knowledge;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.commons.repository.BaseRepository;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.knowledge.KngKnowledgeComment;
import itsm.isperp.module.repository.knowledge.KngKnowledgeCommentMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class KngKnowledgeCommentService extends
		BaseService<KngKnowledgeComment> {

	@Autowired
	protected KngKnowledgeCommentMapper kngKnowledgeCommentMapper;

	@Autowired
	protected KngKnowledgeItemService KngKnowledgeItemService;

	@Override
	public BaseRepository<KngKnowledgeComment> getRepository() {
		return this.kngKnowledgeCommentMapper;
	}

	public ResultMessage addCommont(String knowledgeId, String contents) {

		return addCommont(knowledgeId, contents,
				ContextHolder.getLoginUsername());
	}

	public ResultMessage addCommont(String knowledgeId, String contents,
			String username) {
		KngKnowledgeComment comment = new KngKnowledgeComment();
		comment.setId(HibernateUUIDGenerator.generate());
		comment.setCommentTime(new Date());
		comment.setContent(contents);
		comment.setKngKnowledgeItemid(knowledgeId);
		comment.setLoginUserAccount(username);
		this.getRepository().save(comment);

		// 添加评论后，知识评论次数加1

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		jdbcTemplate.batchUpdate(
				"UPDATE kng_knowledge_item SET commentary_times=1 WHERE id='"
						+ knowledgeId + "' AND commentary_times IS NULL",
				"UPDATE kng_knowledge_item SET commentary_times=commentary_times+1 WHERE id='"
						+ knowledgeId + "'");

		return ResultMessage.success();
	}
	public ResultMessage addCommont(String knowledgeId, String contents, Integer commentType,
      String username) {
KngKnowledgeComment comment = new KngKnowledgeComment();
comment.setId(HibernateUUIDGenerator.generate());
comment.setCommentTime(new Date());
comment.setContent(contents);
comment.setKngKnowledgeItemid(knowledgeId);
comment.setLoginUserAccount(username);
comment.setCommentType(commentType);
this.getRepository().save(comment);

// 添加评论后，知识评论次数加1

JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
jdbcTemplate.batchUpdate(
"UPDATE kng_knowledge_item SET commentary_times=1 WHERE id='"
+ knowledgeId + "' AND commentary_times IS NULL",
"UPDATE kng_knowledge_item SET commentary_times=commentary_times+1 WHERE id='"
+ knowledgeId + "'");

return ResultMessage.success();
}

	public List<Map<String, Object>> findAllDetailByKnowledgeId(
			String knowledgeId) {
		JqGridRequest req = new JqGridRequest();
		req.setPageable(false);
		req.setSort("commentTime", Direction.DESC);
		req.addFilterRule("kngKnowledgeItemid", JqGridFilterRuleOp.eq,
				knowledgeId);
		return this.getRepository().findAllDetailByRequest(req);
	}

}
